//
//  CGQshangxianzhiTableViewCell.swift
//  SheYangBigData
//
//  Created by 叁拾叁 on 2021/5/11.
//  Copyright © 2021 叁拾叁. All rights reserved.
//

import UIKit

class CGQshangxianzhiTableViewCell: SYBaseCell {
    
    var inputClosure: ((String) -> Void)?
    
    let title: UILabel = {
        let v:UILabel = UILabel()
        v.textColor = UIColor(hex: 0x333333)
        v.text = "取值设置"
        v.font = 15.autoBoldFontSize()
        
        return v
    }()
    
    let lowerTf: CGQLabTFView = {
        let v:CGQLabTFView = CGQLabTFView()
        v.title.text = "取值上线:"
        v.num.placeholder = "请输入"
        return v
    }()
    
    let upperTf: CGQLabTFView = {
        let v:CGQLabTFView = CGQLabTFView()
        v.title.text = "取值下线:"
        v.num.placeholder = "请输入"
        return v
    }()
    
    
    
    var sepView: UIView = {
        let v = UIView()
        v.backgroundColor = Constant.bgViewColor
        return v
    }()
    
    
    var lView: UIView = {
        let v = UIView()
        v.backgroundColor = UIColor(hex: 0xE6E6E6)
        return v
    }()
    
    var bgView: UIView = {
        let v = UIView()
        v.backgroundColor = .white
        v.layer.masksToBounds = true
        v.layer.cornerRadius = 5.autoWidth()
        return v
    }()
    
    public func configureView(model: CGQkongzhiqiModel, row: Int) {
        
        switch row {
        case 4:
            title.text = "取值设置"
            upperTf.title.text = "取值上限："
            lowerTf.title.text = "取值下限："
            
            upperTf.num.text = model.availableUpperLimit
            lowerTf.num.text = model.availableLowerLimit
            
            
            lowerTf.num.rx.controlEvent([.editingDidEnd]) //状态可以组合
                .asObservable()
                .subscribe(onNext: { _ in
                    print("结束编辑编辑内容!")
                    self.passParamsClosure?( ["availableLowerLimit":self.lowerTf.num.text ?? "","availableUpperLimit": self.upperTf.num.text ?? ""] as Any)
                    
                }).disposed(by: disposeBag)
            upperTf.num.rx.controlEvent([.editingDidEnd]) //状态可以组合
                .asObservable()
                .subscribe(onNext: { _ in
                    print("结束编辑编辑内容!")
                    self.passParamsClosure?( ["availableLowerLimit":self.lowerTf.num.text ?? "","availableUpperLimit": self.upperTf.num.text ?? ""] as Any)
                    
                }).disposed(by: disposeBag)
            
            
            break
            
        case 5:
            title.text = "预警设置"
            upperTf.title.text = "预警上限："
            lowerTf.title.text = "预警下限："
            upperTf.num.text = model.warnUpperLimit
            lowerTf.num.text = model.warnLowerLimit
            
            lowerTf.num.rx.controlEvent([.editingDidEnd]) //状态可以组合
                .asObservable()
                .subscribe(onNext: { _ in
                    print("结束编辑编辑内容!")
                    self.passParamsClosure?( ["warnLowerLimit":self.lowerTf.num.text ?? "","warnUpperLimit": self.upperTf.num.text ?? ""] as Any)
                    
                }).disposed(by: disposeBag)
            upperTf.num.rx.controlEvent([.editingDidEnd]) //状态可以组合
                .asObservable()
                .subscribe(onNext: { _ in
                    print("结束编辑编辑内容!")
                    self.passParamsClosure?(["warnLowerLimit":self.lowerTf.num.text ?? "","warnUpperLimit": self.upperTf.num.text ?? ""] as Any)
                    
                }).disposed(by: disposeBag)
            
            
            break
            
        default: break
            
        }
        
        
    }
    
    override func initViews() {
        
        
        self.backgroundColor = Constant.bgViewColor
        contentView.addSubview(bgView)
        contentView.addSubview(sepView)
        bgView.addSubview(title)
        bgView.addSubview(lowerTf)
        bgView.addSubview(lineView)
        bgView.addSubview(upperTf)
        bgView.addSubview(lView)
        
        sepView.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.height.equalTo(10.autoWidth())
            make.top.equalToSuperview()
        }
        bgView.snp.makeConstraints { (make) in
            
            make.left.equalToSuperview().offset(15.autoWidth())
            make.right.equalToSuperview().offset(-15.autoWidth())
            make.bottom.equalToSuperview().offset(0)
            make.top.equalTo(sepView.snp.bottom).offset(0)
        }
        title.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(autoWidth(23))
            make.left.equalToSuperview().offset(autoWidth(10))
            make.right.equalToSuperview().offset(autoWidth(-10.0))
            make.height.equalTo(14.autoWidth())
        }
        
        lineView.snp.makeConstraints { (make) in
            make.top.equalTo(title.snp.bottom).offset(autoWidth(14))
            make.left.equalToSuperview()
            make.right.equalToSuperview()
            make.height.equalTo(0.5.autoWidth())
        }
        
        upperTf.snp.makeConstraints { (make) in
            make.top.equalTo(lineView.snp.bottom)
            make.left.equalToSuperview().offset(10.autoWidth())
            make.right.equalToSuperview()
            //            make.height.equalTo(44.autoWidth())
        }
        
        lView.snp.makeConstraints { (make) in
            make.top.equalTo(upperTf.snp.bottom)
            make.left.equalToSuperview()
            make.right.equalToSuperview()
            make.height.equalTo(0.5.autoWidth())
            
            
        }
        lowerTf.snp.makeConstraints { (make) in
            make.top.equalTo(upperTf.snp.bottom)
            make.left.equalToSuperview().offset(10.autoWidth())
            make.right.equalToSuperview()
            make.bottom.equalToSuperview()
        }
    }
}


class CGQLabTFView: SYBaseView {
    
    let title: UILabel = {
        let v = UILabel()
        
        v.font = 14.autoFontSize()
        v.textColor = UIColor(hex: 0x333333)
        v.textAlignment = .left
        return v
    }()
    
    let num: UITextField = {
        let v:UITextField = UITextField()
        v.textColor = UIColor(hex: 0x333333)
        v.font = 14.autoFontSize()
        v.textAlignment = .left
        return v
    }()
    
    
    
    
    
    override func initViews() {
        self.backgroundColor = .clear
        addSubview(title)
        addSubview(num)
        
        title.snp.makeConstraints { (make) in
            make.top.equalToSuperview()
            make.left.equalToSuperview()
            make.height.equalTo(44.autoWidth())
            make.width.equalTo(69.autoWidth())
            make.bottom.equalToSuperview()
        }
        num.snp.makeConstraints { (make) in
            make.top.equalToSuperview()
            make.right.equalToSuperview().offset(0)
            make.left.equalTo(title.snp.right).offset(16.autoWidth())
            make.bottom.equalToSuperview()
            make.height.greaterThanOrEqualTo(13.autoWidth())
        }
        
        
    }
}
